package com.example.q_kang.hhh.setting;

import com.hjq.toast.ToastUtils;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.base.BasePresenter;
import com.sskj.base.api.Constant;
import com.sskj.base.base.HttpData;
import com.sskj.base.bean.UpdataBean;
import com.sskj.base.bean.UploadImgBean;
import com.sskj.base.bean.UserData;
import com.example.q_kang.hhh.api.JsonCallBack;

import java.io.File;


/**
 * @author Hey
 * Create at  2019/04/09
 */
class SettingPresenter extends BasePresenter<SettingFragment> {

    public void getUserData(CoolRefreshView coolRefreshView) {
        httpService.getUserData()
                .tag(this)
                .execute(new JsonCallBack<HttpData<UserData>>() {
                    @Override
                    public void onSuccess(Response<HttpData<UserData>> response) {
                        HttpData<UserData> httpData = response.body();
                        if (httpData.getStatus() == Constant.OK) {
                            if (httpData.getData().getUserinfo() != null) {
                                mView.getUserSuccess(httpData.getData().getUserinfo());
                            }
                        }

                    }

                    @Override
                    public void onStart(Request<HttpData<UserData>, ? extends Request> request) {
                        super.onStart(request);
                        mView.showLoading();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        mView.hideLoading();
                        if (coolRefreshView != null && coolRefreshView.isRefreshing()) {
                            coolRefreshView.setRefreshing(false);
                        }

                    }
                });
    }

    public void uploadImg(File file) {
        httpService.uploadImg(file)
                .tag(this)
                .execute(new JsonCallBack<HttpData<UploadImgBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<UploadImgBean>> response) {
                        HttpData httpData = response.body();
                        if (httpData.getStatus() == Constant.OK) {
                            UploadImgBean uploadImgBean = (UploadImgBean) httpData.getData();
                            changeHead(uploadImgBean.getSrc());
                        }
                    }
                });
    }

    public void changeHead(String src) {
        httpService.changeHead(src)
                .tag(this)
                .execute(new JsonCallBack<HttpData>() {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        ToastUtils.show(httpData.getMsg());
                        if (httpData.getStatus() == Constant.OK) {
                            mView.uploadSuccess(src);
                        }
                    }
                });
    }


    public void updata(String verName) {
        httpService.updata(verName)
                .execute(new JsonCallBack<HttpData<UpdataBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<UpdataBean>> response) {
                        HttpData<UpdataBean> body = response.body();
                        if (body.getStatus() == Constant.OK) {
                            mView.needUpdata(body.getData());
                        } else {
                            ToastUtils.show(body.getMsg());
                        }

                    }

                    @Override
                    public void onStart(Request<HttpData<UpdataBean>, ? extends Request> request) {
                        super.onStart(request);
                        mView.showLoading();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        mView.hideLoading();
                    }
                });
    }
}
